<?php
/*
 *@Note 菜单规则
 * @Date 
 * @Time 
 */

 namespace app\system\controller;


class AuthRule extends Base
{
    // 菜单管理首页
    public function index() {

        return $this->fetch();
    }

    // 菜单添加
    public function add() {
        if(request()->isPost()) {
            $data = input('post.');
            if($data['icon'] != '') $data['icon'] =  'fa ' . $data['icon']; 
            $data['create_at'] = time();
            $res = model('system_menu')->insert($data);
            if($res) {
                system_log(['menu'=> '菜单管理', 'descr'=> '添加菜单', 'create_time' => time() ]);
                return ajaxArray(1,'添加成功');
            } 
            return ajaxArray(0,'添加失败');
        }
        $menus = model('system_menu')->field('id,pid,icon,title')->order('sort asc')->select();
        $menus = $this->menu($menus);
        $this->assign('menus',$menus); // 权限列表
        return $this->fetch();
    }

    // 菜单编辑
    public function edit() {
        $id = input('id/d',0);
        if(request()->isPost()) {
            $data = input('post.');
            unset($data['id']);
            $data['update_at'] = time();
            $res = model('system_menu')->where('id',$id)->update($data);
            system_log(['menu'=> '菜单管理', 'descr'=> '编辑菜单', 'create_time' => time() ]);
            if($res) return ajaxArray(1,'编辑成功');
            return ajaxArray(0,'编辑失败');
        }
        $info = model('system_menu')->where('id',$id)->find();
        $menus = model('system_menu')->field('id,pid,icon,title')->order('sort asc')->select();
        $menus = $this->menu($menus);
        $this->assign('menus',$menus); // 权限列表
        return $this->fetch('',[
            'info' => $info
        ]);
    }

    // 编辑修改
    public function update() {
        if(request()->isPost()) {
            $data = input('post.');
            $res = model('system_menu')->where('id','eq', $data['id'])->update([$data['field'] => $data['value']]);
            if($res) {
                return ajaxArray(1,'修改成功');
            }
            return ajaxArray(0,'请重新编辑');
        }
    }
  
    // 类型组装
    private function menu($cate, $lefthtml='|— ', $pid=0, $lvl=0){
        $arr = [];
        foreach ($cate as $v){
            if($v['pid'] == $pid){
                $v['lefthtml'] = str_repeat($lefthtml,$lvl);
                $v['ltitle'] = $v['lefthtml'].$v['title'];
                $v['icon'] = str_replace("&","&amp;",$v['icon']);
                $arr[] = $v;
                $arr = array_merge($arr, $this->menu($cate, $lefthtml, $v['id'], $lvl+1));
            }
        }
        return $arr;
    }

    // 输出接口
    public function menus() {
        $list = model('system_menu')->field('id,pid,title,icon,href,sort,target,status')
        ->order('sort', 'desc')
        ->select();
        $count = model('system_menu')->count();
        $data['code'] = 0;
        $data['msg'] = '获取成功';
        $data['data'] = $list;
        $data['count'] = $count;
        return json($data);
    }
}